<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Gen Page menu fragment
 *
 * Usage: php index.php tools/menu gen
 */

/*
 * $Id: menu.php 60 2011-08-14 09:34:44Z leikou@163.com $
 */


class Menu extends CI_Controller
{
    private $_menu_data;
    private $_class_func_role;


    public function gen()
    {
        $this->_get_data();
        $this->_get_class_func_roles();
        list($level_1_roles, $level_2_roles, $level_3_roles) = $this->_get_menu_roles();
       
        $template_str = $this->load->smarty('menu_template', array(
            'oa_menu' => $this->_menu_data,
            'level_1_roles' => $level_1_roles,
            'level_2_roles' => $level_2_roles,
            'level_3_roles' => $level_3_roles,
        ), TRUE);

        $this->_write_file($template_str);
    }


    private function _get_data()
    {
        require_once APPPATH.'controllers/include/menu_data.inc.php';
        $this->_menu_data =& $oa_menu_data;
    }


    private function _get_class_func_roles()
    {
        // Get all menu class func 
        $where_array = array();
        foreach($this->_menu_data as $level_1)
        {
            foreach($level_1['sub_menu'] as $level_2)
            {
                foreach($level_2['sub_menu'] as $level_3)
                {
                    $class = $level_3['class'];
                    $func = $level_3['func'];
                    $this->_class_func_role[$class.'/'.$func] = array();
                    $where_array["(cf.class = '$class' AND cf.func = '$func' )"] = NULL;
                }
            }
        }

        // Get role array from db
        $query = $this->db
            ->select('cf.class, cf.func, r.r_id')
            ->from('class_func_description AS cf')
            ->join('class_func_role AS r', 'cf.id = r.cf_id')
            ->or_where($where_array)
            ->get();

        foreach($query->result_array() as $row)
        {
            $key = $row['class'].'/'.$row['func'];
            $this->_class_func_role[$key][] = $row['r_id'];
        }
    }


    private function _get_menu_roles()
    {
        // Caculate menu role limitation
        $level_1_roles = array();
        $level_2_roles = array();
        $level_3_roles = array();

        foreach($this->_menu_data as $level_1)
        {
            $level_1_segment = $level_1['segment'];
            $level_1_roles[$level_1_segment] = array();
            foreach($level_1['sub_menu'] as $level_2)
            {
                $level_2_segment = $level_2['segment'];
                $level_2_roles[$level_1_segment][$level_2_segment] = array();
                foreach($level_2['sub_menu'] as $level_3)
                {
                    $level_3_segment = $level_3['segment'];
                    $level_3_roles[$level_1_segment][$level_2_segment][$level_3_segment] = array();

                    $class = $level_3['class'];
                    $func = $level_3['func'];
                    $roles = $this->_class_func_role[$class.'/'.$func];
                    if(empty($roles))
                    {
                        continue;
                    }

                    $level_1_roles[$level_1_segment] = array_merge(
                        $level_1_roles[$level_1_segment], $roles
                    );
                    $level_2_roles[$level_1_segment][$level_2_segment] = array_merge(
                        $level_2_roles[$level_1_segment][$level_2_segment], $roles
                    );
                    $level_3_roles[$level_1_segment][$level_2_segment][$level_3_segment] = $roles;
                }

                $level_2_roles[$level_1_segment][$level_2_segment] = 
                    array_unique($level_2_roles[$level_1_segment][$level_2_segment]);
            }

            $level_1_roles[$level_1_segment] = array_unique($level_1_roles[$level_1_segment]);
        }

        return array($level_1_roles, $level_2_roles, $level_3_roles);
    }


    private function _write_file($template_str)
    {
        $template_path = REALAPPPATH.'views/include/menu.tpl';
        $fp = fopen($template_path, 'w+');
        if(fwrite($fp, $template_str) === FALSE)
        {
            die('Can not write to file '.$template_path);
        }

        fclose($fp);
    }
}



/* End of file: menu.php */
/* Location ./application/tools/menu.php */
